package com.lzy.mall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.product.entity.PmsAttr;
import com.lzy.mall.product.entity.PmsAttrGroup;
import com.lzy.mall.product.dao.PmsAttrGroupDao;
import com.lzy.mall.product.service.PmsAttrAttrgroupRelationService;
import com.lzy.mall.product.service.PmsAttrGroupService;
import com.lzy.mall.product.service.PmsAttrService;
import com.lzy.mall.product.domain.PmsEnum;
import com.lzy.mall.product.vo.AttrGroupWithAttrsVo;
import com.lzy.mall.product.vo.SkuItemVo;
import com.lzy.mall.product.vo.SpuItemAttrGroup;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 属性分组(PmsAttrGroup)表服务实现类
 *
 * @author lzy
 * @since 2021-08-25 11:41:09
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PmsAttrGroupServiceImpl implements PmsAttrGroupService {
    private final PmsAttrGroupDao pmsAttrGroupDao;

    private final PmsAttrAttrgroupRelationService relationService;

    private final PmsAttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> map, Long cateLogId) {
        String key = (String) map.get("key");
        PageQuery pageQuery = new PageQuery();
        if (StrUtil.isNotBlank(key)){
            pageQuery.setPara("key",key);
        }
        if (cateLogId != 0){
            pageQuery.setPara("id",cateLogId);
        }
        PageQuery<PmsAttrGroup> page = pmsAttrGroupDao.queryPage(pageQuery);

        return new PageUtils(page);
    }

    /**
     * 根据分类id查出所有的分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1、查询分组信息
        List<PmsAttrGroup> select = pmsAttrGroupDao.createLambdaQuery()
                .andEq(PmsAttrGroup::getCatelogId, catelogId)
                .andEq(PmsAttrGroup::getType, PmsEnum.ATTR_TYPE_BASE.getCode())
                .select();
        //2、查询所有属性
        List<AttrGroupWithAttrsVo> vos = select.stream().map(group -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtil.copyProperties(group, vo);
            List<PmsAttr> attrs = attrService.getRelationAttr(vo.getAttrGroupId());
            vo.setAttrs(attrs);
            return vo;
        }).collect(Collectors.toList());

        return vos;
    }

    @Override
    public void save(PmsAttrGroup attrGroup) {
        pmsAttrGroupDao.insertTemplate(attrGroup);
    }

    @Override
    public void updateById(PmsAttrGroup attrGroup) {
        pmsAttrGroupDao.updateById(attrGroup);
    }

    @Override
    public void removeByIds(List<Long> ids) {
        pmsAttrGroupDao.createLambdaQuery()
                .andIn(PmsAttrGroup::getAttrGroupId,ids)
                .delete();
    }

    /**
     * 查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
     *
     * @param id
     * @param spuId
     * @return
     */
    @Override
    public List<SpuItemAttrGroup> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        List<SpuItemAttrGroup> attrGroups = pmsAttrGroupDao.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return attrGroups;
    }
}
